/**
 * 
 */
package com.zhiteng.listener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.zhiteng.entity.SysDict;
import com.zhiteng.mapper.SysDictMapper;
import com.zhiteng.utils.common.DictUtils;

/**  
* <p>Title: StartSystemListener.java</p>  
* <p>Description: </p>  
* @author dragon  
* @date 2018年12月2日  
* @version 1.0  
*/
public class StartSystemListener implements ServletContextListener {
	
	//	在服务器启动时,创建application对象时需要执行的方法.
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		/*ServletContext application = sce.getServletContext();
		ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(application);
		SysDictMapper dictMapper = ac.getBean(SysDictMapper.class);
		
		Map<String, List<SysDict>> dictMap = new HashMap<String, List<SysDict>>();
		
		// 系统启动时，缓存字典，并且每隔1h更新一次
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				
				List<SysDict> sysDicts = dictMapper.selectAll();
				// 每次做字典缓存时都需要把之前的缓存先清空
				dictMap.clear();
				for (SysDict sysDict : sysDicts) {
					if (dictMap.containsKey(sysDict.getType())) {
						// map中已经存在该key
						dictMap.get(sysDict.getType()).add(sysDict);
					} else {
						// map中还没有该key
						List<SysDict> dicts = new ArrayList<SysDict>();
						dicts.add(sysDict);
						dictMap.put(sysDict.getType(), dicts);
					}
				}
				application.setAttribute("dictMap", dictMap);
			}

		}, 0, 1000 * 60 * 15);*/
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {

	}

}
